#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@File    :   getallimg.py
@Time    :   2024/12/15 23:05:42
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

from time import sleep

import ddddocr
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from utils.DriverUtils import wait_page_all_load
from utils.my_error import MyError


def get_image(url, pic_name):

    # chromedriver的路径
    chrome_service = Service(
        './webdriver/chromedriver_131.0.6778.85_x32.exe')
# 设置chrome开启的模式，headless就是无界面模式
# 一定要使用这个模式，不然截不了全页面，只能截到你电脑的高度
    chrome_options = webdriver.ChromeOptions()
    # chrome_options = Options()
    chrome_options.add_argument('headless')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.page_load_strategy = 'none'
    chrome_options.add_experimental_option(
        "excludeSwitches", ['ignore-certificate-errors', 'ignore-ssl-errors'])
    driver = webdriver.Chrome(service=chrome_service,
                              options=chrome_options)

# 控制浏览器写入并转到链接
    driver.get(url)
    wait_page_all_load(driver)  # 留时间给页面加载
# 接下来是全屏的关键，用js获取页面的宽高，如果有其他需要用js的部分也可以用这个方法
    width = driver.execute_script(
        "return document.documentElement.scrollWidth")
    height = driver.execute_script(
        "return document.documentElement.scrollHeight")
# 将浏览器的宽高设置成刚刚获取的宽高
    driver.set_window_size(width, height)
    sleep(1)
# 截图并关掉浏览器
    driver.save_screenshot(pic_name)
    driver.close()


get_image('https://mp.weixin.qq.com/s?__biz=MzU5MzcyNDg3OA==&mid=2247486672&idx=1&sn=53f6fa0917ed68d99d2bb24ba92b79bb&chksm=fe0d5871c97ad167f7ccf4f71ce67622a4a5d602382d7cd4d9a0573ba58116177dc13c3eec4b&cur_album_id=1908398100679835649&scene=190#rd', 'all.png')
